<?php
	@session_start();
	require_once "settings.php";
	class dataAccess extends CONFIGURE
	{
		private $conn = null;
		
		//open connect as default
		function __construct()
		{				
			$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_password);			
			if (!$this->conn)
			{		  				  		
				die('Could not connect: ' . mysql_error());
				//DATABASE NAME HERE - you can change to wharever as you want		  		
			}		  	
			mysql_select_db($this->db_name) or die(mysql_error());						
		}
		
		public function addNewTerm($term, $definition, $url)
		{
			$term = $this->getDoupleQuoteText($term);
			$definition = $this->getDoupleQuoteText($definition);
			$url = $this->getDoupleQuoteText($url);
			$query = "INSERT INTO terms (term, definition, url) VALUES ('$term', '$definition', '$url')";
            mysql_query($query);
		}
		
		public function getAllTerms()
		{
			$query = "SELECT * FROM terms";
            $data = mysql_query($query);
            return $data;
		}
		
		public function searchTerm($term)
		{
			$data = null;
			if (strlen($term) > 0)
			{
				$condition = $this->getDoupleQuoteText($term);				
				$query = "SELECT * FROM terms WHERE term = '$condition'";				
	            $data = mysql_query($query);
			}
            return $data;
		}
		
		public function clearAllTerms()
		{			
			$query = "TRUNCATE TABLE terms";
            mysql_query($query);
		}
		
		public function addPdfTerm($content, $url, $year, $uploader)
		{
			$content = $this->getDoupleQuoteText($content);
			$url = $this->getDoupleQuoteText($url);
			$query = "INSERT INTO pdfterms (content, url, year, uploader) VALUES ('$content', '$url', '$year','$uploader')";
            $data = mysql_query($query);            
		}
		
		public function addPdfWordTerm($content, $url, $year, $uploader, $uid) {
			$content = $this->getDoupleQuoteText($content);
			$url = $this->getDoupleQuoteText($url);
			$sql = "INSERT INTO pdfterms set content='".$content."', url='".$url."', year='".$year."', uploader='".$uploader."', user_id='".$uid."'";
			$data = mysql_query($sql);
		}
		
		public function insertComposeTemp2($type, $title, $content, $uid) {
			$sql = "INSERT INTO composedocument_temp set comments='".$content."',assignmenttitle = '" . $title . "', username='".$uid."', types='".$type."'";
			$res = mysql_query($sql);
		}
		
		public function deleteComposeTemp2($id)
		{
			$quoteID = $this->getDoupleQuoteText($id);
			$sql = "DELETE FROM composedocument_temp WHERE id='$quoteID'";			
			$res = mysql_query($sql);
		}
		
		public function insertComposeTemp($type, $data) {
			if($type == 0) { // book
				$loai = 'book';
				$title = $this->getDoupleQuoteText($data['title1']);
				$journal = '';
				$date = $this->getDoupleQuoteText($data['date1']);
				$pcity = $this->getDoupleQuoteText($data['pcity1']);
				$page = $this->getDoupleQuoteText($data['pages1']);
				$edition = $this->getDoupleQuoteText($data['edition1']);
				$firstname = $this->getDoupleQuoteText($data['firstname1']);
				$lastname = $this->getDoupleQuoteText($data['lastname1']);
			} else if($type == 1) { // journal
				$loai = "journal";
				$title = $this->getDoupleQuoteText($data['title2']);
				$journal = $this->getDoupleQuoteText($data['journal2']);
				$date = $this->getDoupleQuoteText($data['date2']);
				$pcity = $this->getDoupleQuoteText($data['pcity2']);
				$page = $this->getDoupleQuoteText($data['pages2']);
				$edition = $this->getDoupleQuoteText($data['edition2']);
				$firstname = $this->getDoupleQuoteText($data['firstname2']);
				$lastname = $this->getDoupleQuoteText($data['lastname2']);
			}
			$atitle = $this->getDoupleQuoteText($data['atitle']);
			$pub = $this->getDoupleQuoteText($data['pub1']);
			$issue = $this->getDoupleQuoteText($data['issue2']);
			$digital = $this->getDoupleQuoteText($data['digital2']);
			$sql = "INSERT INTO composedocument_temp (
					types,
					assignmenttitle,
					booktitle,
					journaltitle,
					publisher,
					pcity,
					pyear,
					edition,
					issuenumber,
					pagenumber,
					digitalobject,
					firstname,
					lastname,
					author,
					comments,
					uploads,
					username,
					reference
				) VALUES (
					'".$loai."',
					'".$atitle."',
					'".$title."',
					'".$journal."',
					'".$pub."',
					'".$pcity."',
					'".$date."',
					'".$edition."',
					'".$issue."',
					'".$page."',
					'".$digital."',
					'".$firstname."',
					'".$lastname."',
					'',
					'',
					'".$data['fileupload']."',
					'".$data['userid']."',
					''
				)";
			$res = mysql_query($sql);
		}
		
		public function insertComposeMain2($type, $data) {
			if($type == 0) { // book
				$loai = 'book';
				$title = $this->getDoupleQuoteText($data['title1']);
				$journal = '';
				$date = $this->getDoupleQuoteText($data['date1']);
				$pcity = $this->getDoupleQuoteText($data['pcity1']);
				$page = $this->getDoupleQuoteText($data['pages1']);
				$edition = $this->getDoupleQuoteText($data['edition1']);
				$firstname = $this->getDoupleQuoteText($data['firstname1']);
				$lastname = $this->getDoupleQuoteText($data['lastname1']);
			} else if($type == 1) { // journal
				$loai = "journal";
				$title = $this->getDoupleQuoteText($data['title2']);
				$journal = $this->getDoupleQuoteText($data['journal2']);
				$date = $this->getDoupleQuoteText($data['date2']);
				$pcity = $this->getDoupleQuoteText($data['pcity2']);
				$page = $this->getDoupleQuoteText($data['pages2']);
				$edition = $this->getDoupleQuoteText($data['edition2']);
				$firstname = $this->getDoupleQuoteText($data['firstname2']);
				$lastname = $this->getDoupleQuoteText($data['lastname2']);
			}
			$atitle = $this->getDoupleQuoteText($data['atitle']);
			$pub = $this->getDoupleQuoteText($data['pub1']);
			$issue = $this->getDoupleQuoteText($data['issue2']);
			$digital = $this->getDoupleQuoteText($data['digital2']);
			$sql = "INSERT INTO composedocument (
					types,
					assignmenttitle,
					booktitle,
					journaltitle,
					publisher,
					pcity,
					pyear,
					edition,
					issuenumber,
					pagenumber,
					digitalobject,
					firstname,
					lastname,
					author,
					comments,
					uploads,
					username,
					reference,
					title_reference
				) VALUES (
					'".$loai."',
					'".$atitle."',
					'".$title."',
					'".$journal."',
					'".$pub."',
					'".$pcity."',
					'".$date."',
					'".$edition."',
					'".$issue."',
					'".$page."',
					'".$digital."',
					'".$firstname."',
					'".$lastname."',
					'',
					'',
					'".$data['fileupload']."',
					'".$data['userid']."',
					'',
					'".$data['title_reference']."'
				)";
			//echo $sql;exit;	
			$res = mysql_query($sql);
		}
		
		public function updateComposeTemp($type, $data) {
			if($type == 0) { // book
				$loai = 'book';
				$title = $this->getDoupleQuoteText($data['title1']);
				$journal = '';
				$date = $this->getDoupleQuoteText($data['date1']);
				$pcity = $this->getDoupleQuoteText($data['pcity1']);
				$page = $this->getDoupleQuoteText($data['pages1']);
				$edition = $this->getDoupleQuoteText($data['edition1']);
				$firstname = $this->getDoupleQuoteText($data['firstname1']);
				$lastname = $this->getDoupleQuoteText($data['lastname1']);
			} else if($type == 1) { // journal
				$loai = "journal";
				$title = $this->getDoupleQuoteText($data['title2']);
				$journal = $this->getDoupleQuoteText($data['journal2']);
				$date = $this->getDoupleQuoteText($data['date2']);
				$pcity = $this->getDoupleQuoteText($data['pcity2']);
				$page = $this->getDoupleQuoteText($data['pages2']);
				$edition = $this->getDoupleQuoteText($data['edition2']);
				$firstname = $this->getDoupleQuoteText($data['firstname2']);
				$lastname = $this->getDoupleQuoteText($data['lastname2']);
			}
			$atitle = $this->getDoupleQuoteText($data['atitle']);
			$pub = $this->getDoupleQuoteText($data['pub1']);
			$issue = $this->getDoupleQuoteText($data['issue2']);
			$digital = $this->getDoupleQuoteText($data['digital2']);
			$sql = "UPDATE composedocument_temp SET
					types='".$loai."',
					assignmenttitle='".$atitle."',		
					booktitle='".$title."',
					journaltitle='".$journal."',
					publisher='".$pub."',
					pcity='".$pcity."',
					pyear='".$date."',,
					edition='".$edition."',
					issuenumber='".$issue."',
					pagenumber='".$page."',
					digitalobject='".$digital."',
					firstname='".$firstname."',
					lastname='".$lastname."',
					author='',
					comments='',
					username='".$data['userid']."',
					WHERE uploads='".$data['fileupload']."'";
			$res = mysql_query($sql);
		}
		
		public function updatePdfTerms_temp($tempid, $filename) {
			$sql = "UPDATE pdfterms SET temp_compose_id='".$tempid."' WHERE url='".$filename."'";
			$res = mysql_query($sql);
		}
		
		public function updatePdfTerms_main($mainid, $filename) {
			$sql = "UPDATE pdfterms SET main_compose_id='".$mainid."', status='1' WHERE url='".$filename."'";
			$res = mysql_query($sql);
		}
		
		public function updatePdfTerms_doing_later($tempid, $filename) {
			$sql = "UPDATE pdfterms SET temp_id_continue='".$tempid."' WHERE url='".$filename."'";
			$res = mysql_query($sql);
		}
		
		public function deletePdfTerms_doing_later($tempid) {
			$tempid = $this->getDoupleQuoteText($tempid);
			$sql = "DELETE FROM pdfterms SET id='" . $tempid. "'";
			$res = mysql_query($sql);
		}

		public function remove_temp($filename) {
			$sql = "DELETE FROM composedocument_temp WHERE uploads='".$filename."'";
			$res = mysql_query($sql);
		}
		
		public function check_on_doing_assignment($uid) {
			$sql = "SELECT * FROM composedocument_temp WHERE username='".$uid."'";
			$res = mysql_query($sql);
			$num = mysql_num_rows($res);
			return $num;
		}
		
		public function checkFileUploadExist_ComposeTemp($fileupload, $uid) {
			$sql = "SELECT * FROM composedocument_temp WHERE username='".$uid."' AND uploads='".$fileupload."'";
			$res = mysql_query($sql);
			$num = mysql_num_rows($res);
			return $num;
		}
		
		public function insertComposeMain($filename) {
			$sql = "INSERT INTO composedocument(
				types,
				assignmenttitle,
				booktitle,
				journaltitle,
				publisher,
				pcity,
				pyear,
				edition,
				issuenumber,
				pagenumber,
				digitalobject,
				firstname,
				lastname,
				author,
				comments,
				uploads,
				username,
				reference,
				title_reference
			) SELECT t.types, t.assignmenttitle, t.booktitle, t.journaltitle, t.publisher, t.pcity, t.pyear, t.edition, t.issuenumber, t.pagenumber, t.digitalobject, 
			t.firstname, t.lastname, t.author, t.comments, t.uploads, t.username, t.reference, '0' FROM composedocument_temp t 
			WHERE t.uploads='".$filename."'";
			//echo $sql;exit;
			$res = mysql_query($sql);
		}
		
		public function insertCompose3($atitle, $dets, $userid, $tr_post) {
			$sql = "insert into composedocument set assignmenttitle='".$atitle."', comments='".$dets."',username='".$userid."',types='compose', title_reference='".$tr_post."'";
			$res = mysql_query($sql);
			return $res;
		}
		
		public function updateCompose3($atitle, $dets, $tr_post, $aid) {
			$sql = "UPDATE composedocument SET assignmenttitle='".$atitle."', comments='".$dets."', title_reference='".$tr_post."' WHERE Id='".$aid."'";
			//echo $sql;exit;
			$res = mysql_query($sql);
			return $res;
		}
		
		public function last_inserted_id() {
			$id = mysql_insert_id();
			return $id;
		}
		
		public function searchPdfTerms($condition, $userid)
		{
			$data = null;
			if (strlen($condition) > 0)
			{
				$condition = $this->getDoupleQuoteText($term);				
				$query = "SELECT p.url, p.content, p.year, c.firstname FROM pdfterms p INNER JOIN composedocument c ON p.main_compose_id = c.id WHERE p.user_id='$userid' AND p.content LIKE '%$condition%'";				
	            $data = mysql_query($query);
			}
            return $data;
		}
		
		public function getAllPdfTerms()
		{
			$query = sprintf("SELECT * FROM pdfterms");
            $data = mysql_query($query);
            return $data;
		}
		
		public function clearAllPdfTerms()
		{			
			$query = "TRUNCATE TABLE pdfterms";
            mysql_query($query);
		}
		
		function  __destruct()
		{
			if ($this->conn)
			{
				mysql_close($this->conn);
			}
		}

		function getDoupleQuoteText($text)
		{
			return str_replace("'", "''", $text);
		}
		
		public function getFirstNameByID($id)
		{
			$data = null;
			if (strlen($id) > 0)
			{								
				$query = "SELECT * FROM users WHERE Id = '$id'";				
	            $result = mysql_query($query);	            
	            while($row = mysql_fetch_array($result))
				{
					$data = $row['FirstName'];					
				}	            
			}
            return $data;		
		}
		
		public function getUsersOnline()
		{	
			if ( isset($_SERVER["REMOTE_ADDR"]) )    { 
				$ip=$_SERVER["REMOTE_ADDR"] . ' '; 
			} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    { 
				$ip=$_SERVER["HTTP_X_FORWARDED_FOR"] . ' '; 
			} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    { 
				$ip=$_SERVER["HTTP_CLIENT_IP"] . ' '; 
			} 
						
			$tg=time();
			$time_out = 1800;//15 minutes			
			$time_new = $tg - $time_out;
			if (isset($ip))
			{
				$sql="insert into users_online(time,ip,url) values('$tg', '$ip' ,'$PHP_SELF')";			
				$query=mysql_query($sql);			
			}
			
			$sql="delete from users_online where time < $time_new";			
			$query=mysql_query($sql);			
			$sql="SELECT COUNT(DISTINCT ip) FROM users_online";			
			$query= mysql_query($sql);			
			$result = mysql_result($query, 0, 0);			
			return $result;
		}
		
		public function deleteAssigmnent($id)
		{	
			$sql="delete from composedocument where id='".$id."'";
			$query = mysql_query($sql);
			return $query;
		}
		
		public function deleteTemp_data_offline($uid) {
			$sql="delete from temp_data_offline where userid = '" . $uid . "'";
			$query = mysql_query($sql);
			return $query;
		}
		
		public function edit_title($id, $txt, $u) {
			$txt = $this->getDoupleQuoteText($txt);
			$sql = "UPDATE tbl_references SET Title_Reference='".$txt."', User='".$u."', UpdatedDate='".date("Y-m-d H:i", time())."' WHERE ID='".$id."' ";
			$res = mysql_query($sql);
			return $res;
		}
		
		public function search_city_by_publisher($publisher_name) {
			$condition = $this->getDoupleQuoteText($publisher_name);				
			$query = "SELECT * FROM publisher_city WHERE publisher_name LIKE '%" . $condition . "%'";
			$data = mysql_query($query);
			$result = "";
			
			while($row = mysql_fetch_array($data))
			{
				$result = $row['city'];					
				break;
			}	            				
			
			return $result;
		}
		
		public function clear_old_history_login($session_id = null)
		{		
			$dt=time();
			$time_out = 1800;//15 minutes			
			$time_new = $dt - $time_out;			
			
			//clear old history login
			$sql = "";
			
			if ($session_id)
			{
				$sql = "update history_login set active = 0 where datetime < $time_new OR session_id ='$session_id' and active = 1";		
			}
			else
			{
				$sql = "update history_login set active = 0 where datetime < $time_new and active = 1";		
			}			
			$query=mysql_query($sql);						
		}
		
		
	}
?>